<h2>Week Ending: <?php echo date('F j, Y',$weekending) ?></h2>

<?php
$taskbyclient = array();
foreach($tasks as $task):
	$taskbyclient[$task['Task']['client_id']][] = $task['Task'];
endforeach;
unset($tasks);

$holidaysbydate = array();
foreach($holidays as $holiday):
	$startdate = $holiday['Holiday']['start_date'];
	$holidaysbydate[$startdate] = $holiday['Holiday'];
endforeach;
unset($holidays);

/*echo '<pre>';
print_r($taskbyclient);
echo '<pre>';*/
?>


<?php //echo $this->Form->create('Timesheet');
echo $this->Form->create('Timesheet',array('onsubmit'=>'return validateForm();')) ?>
<table width="100%" border="0" cellspacing="1">
  <tr>
    <td valign="bottom">
    	<select name="clients" onchange="javascript:getTasks(this.value)">
            <option value="">Select client</option>
			<?php
            $clientsbyclient = array();
            foreach($clients as $client):?>
                <option value="<?php echo $client['Client']['id']?>"><?php echo $client['Client']['title']?></option>
                <?php $clientsbyclient[$client['Client']['id']][] = $client['Client']; ?>
            <?php endforeach; ?>
            <?php unset($clients); ?>
        </select>
        
        <select name="tasks" id="tasks" onchange="javascript:addTask(this.value)">
            <option>No client selected</option>
        </select>
    </td>
    <td width="80"><?php echo $this->Form->end(__('Submit')); ?></td>
  </tr>
</table>

<table width="100%" border="0" cellspacing="1" class="table">
	<tr>
    <th>Client</th>
    
	<?php
	$oneday = 60*60*24;
	$days = 6;
	$holidaysthisweek = array();
	for($i = $days; $i>=0; $i--):
		$thisday = $weekending - $i*$oneday;
		if(array_key_exists(date('Y-m-d',$thisday), $holidaysbydate)){
			$holidaysthisweek[date('l', $thisday)] = 'readonly="readonly"';
			$holiday = $holidaysbydate[date('Y-m-d',$thisday)]['title'];
		}else{
			$holidaysthisweek[date('l', $thisday)] = '';
			$holiday = '';
		}
		?>
        <th class="colum-thin" valign="bottom"><?php 
		echo '<p class="holiday">'.$holiday.'</p>';
        echo date('D', $thisday);
        echo '<br />';
        echo date('jS',$thisday); ?></th>
	<?php endfor; ?>
    
    <th class="colum-thin">TOTAL</th>
    </tr>
  
  
<tr>
  <td style="padding:30px 0;" class="total">TOTAL<p></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][mon]" id="total_mon" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][tue]" id="total_tue" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][wed]" id="total_wed" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][thur]" id="total_thur" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][fri]" id="total_fri" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][sat]" id="total_sat" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][sun]" id="total_sun" class="textfieldsmall" value="0"></td>
  <td class="total"><input type="text" readonly="" name="data[content][total][total]" id="total_total" class="textfieldsmall" value="0"></td>
</tr>
 
</table>

<script>
var clients = new Array();
clients = <?php echo json_encode($clientsbyclient); ?>;

var tasks = new Array();
tasks = <?php echo json_encode($taskbyclient); ?>;

var holidaysthisweek = new Array();
holidaysthisweek = <?php echo json_encode($holidaysthisweek); ?>;

//console.log(holidaysthisweek);

</script>